package com.codeest.geeknews.ui.main.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.codeest.geeknews.R;
import com.codeest.geeknews.app.Constants;
import com.codeest.geeknews.base.SimpleActivity;
import com.codeest.geeknews.ui.gank.fragment.GankMainFragment;
import com.codeest.geeknews.ui.gold.fragment.GoldMainFragment;
import com.codeest.geeknews.ui.main.fragment.AboutFragment;
import com.codeest.geeknews.ui.main.fragment.LikeFragment;
import com.codeest.geeknews.ui.main.fragment.SettingFragment;
import com.codeest.geeknews.ui.vtex.fragment.VtexMainFragment;
import com.codeest.geeknews.ui.wechat.fragment.WechatMainFragment;
import com.codeest.geeknews.ui.zhihu.fragment.ZhihuMainFragment;
import com.yalantis.guillotine.animation.GuillotineAnimation;
import com.yalantis.guillotine.interfaces.GuillotineListener;

import butterknife.BindView;
import me.yokeyword.fragmentation.SupportFragment;

/**
 * Created by Dmytro Denysenko on 5/4/15.
 */
public class MainActivity2 extends SimpleActivity {
    private static final long RIPPLE_DURATION = 250;


    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.root)
    FrameLayout root;
    @BindView(R.id.content_hamburger)
    View contentHamburger;

    //
    GuillotineAnimation animation;
    boolean isOpened;
    //
    ZhihuMainFragment mZhihuFragment;
    GankMainFragment mGankFragment;
    WechatMainFragment mWechatFragment;
    GoldMainFragment mGoldFragment;
    VtexMainFragment mVtexFragment;
    LikeFragment mLikeFragment;
    SettingFragment mSettingFragment;
    AboutFragment mAboutFragment;
    //
    private int hideFragment = Constants.TYPE_ZHIHU;
    private int showFragment = Constants.TYPE_ZHIHU;

    /**
     * 由于recreate 需要特殊处理夜间模式
     *
     * @param savedInstanceState
     */
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showHideFragment(getTargetFragment(showFragment), getTargetFragment(hideFragment));
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_main2;
    }

    @Override
    protected void initEventAndData() {
        View guillotineMenu = LayoutInflater.from(this).inflate(R.layout.guillotine, null);
        root.addView(guillotineMenu);
        guillotineMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        animation = new GuillotineAnimation.GuillotineBuilder(guillotineMenu, guillotineMenu.findViewById(R.id.guillotine_hamburger), contentHamburger)
                .setStartDelay(RIPPLE_DURATION)
                .setDuration(500)
                .setActionBarViewForAnimation(mToolbar)
                //.setInterpolator(new LinearInterpolator())
                .setClosedOnStart(true)
                .setGuillotineListener(new GuillotineListener() {
                    @Override
                    public void onGuillotineOpened() {
                        isOpened = true;
                    }

                    @Override
                    public void onGuillotineClosed() {
                        isOpened = false;
                    }
                })
                .build();
        //
        //setToolBar(mToolbar, "知乎日报");
        setTitle("知乎日报");
        mZhihuFragment = new ZhihuMainFragment();
        mGankFragment = new GankMainFragment();
        mWechatFragment = new WechatMainFragment();
        mGoldFragment = new GoldMainFragment();
        mVtexFragment = new VtexMainFragment();
        mLikeFragment = new LikeFragment();
        mSettingFragment = new SettingFragment();
        mAboutFragment = new AboutFragment();
        loadMultipleRootFragment(R.id.fl_main_content, 0, mZhihuFragment, mWechatFragment, mGankFragment, mGoldFragment, mVtexFragment, mLikeFragment, mSettingFragment, mAboutFragment);

    }

    private void setTitle(String title) {
        ((TextView) mToolbar.findViewById(R.id.title)).setText(title);
    }

    public void onInformationClick(View view) {
        handleClick(view);
    }

    public void onSettingClick(View view) {
        handleClick(view);
    }

    public void onOtherClick(View view) {
        handleClick(view);
    }

    private void handleClick(View view) {
        switch (view.getId()) {
            case R.id.drawer_zhihu:
                showFragment = Constants.TYPE_ZHIHU;
                break;
            case R.id.drawer_gank:
                showFragment = Constants.TYPE_GANK;
                break;
            case R.id.drawer_wechat:
                showFragment = Constants.TYPE_WECHAT;
                break;
            case R.id.drawer_gold:
                showFragment = Constants.TYPE_GOLD;
                break;
            case R.id.drawer_vtex:
                showFragment = Constants.TYPE_VTEX;
                break;
            case R.id.drawer_setting:
                showFragment = Constants.TYPE_SETTING;
                break;
            case R.id.drawer_like:
                showFragment = Constants.TYPE_LIKE;
                break;
            case R.id.drawer_about:
                showFragment = Constants.TYPE_ABOUT;
                break;
        }
        setTitle((String) view.getTag());
        showHideFragment(getTargetFragment(showFragment), getTargetFragment(hideFragment));
        hideFragment = showFragment;
        animation.close();
    }

    private SupportFragment getTargetFragment(int item) {
        switch (item) {
            case Constants.TYPE_ZHIHU:
                return mZhihuFragment;
            case Constants.TYPE_GANK:
                return mGankFragment;
            case Constants.TYPE_WECHAT:
                return mWechatFragment;
            case Constants.TYPE_GOLD:
                return mGoldFragment;
            case Constants.TYPE_VTEX:
                return mVtexFragment;
            case Constants.TYPE_LIKE:
                return mLikeFragment;
            case Constants.TYPE_SETTING:
                return mSettingFragment;
            case Constants.TYPE_ABOUT:
                return mAboutFragment;
        }
        return mZhihuFragment;
    }

    private int getCurrentItem(int item) {
        switch (item) {
            case Constants.TYPE_ZHIHU:
                return R.id.drawer_zhihu;
            case Constants.TYPE_GANK:
                return R.id.drawer_gank;
            case Constants.TYPE_WECHAT:
                return R.id.drawer_wechat;
            case Constants.TYPE_GOLD:
                return R.id.drawer_gold;
            case Constants.TYPE_VTEX:
                return R.id.drawer_vtex;
            case Constants.TYPE_LIKE:
                return R.id.drawer_like;
            case Constants.TYPE_SETTING:
                return R.id.drawer_setting;
            case Constants.TYPE_ABOUT:
                return R.id.drawer_about;
        }
        return R.id.drawer_zhihu;
    }

    @Override
    public void onBackPressedSupport() {
        if (isOpened) {
            animation.close();
        } else {
            super.onBackPressedSupport();
        }
    }
}
